class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int,int> hash;
        int left=0;
        int right=0;
        int ret=0;
        int n=fruits.size();
        while(right<n)
        {
            hash[fruits[right]]++;
            while(hash.size()>2)
            {
                hash[fruits[left]]--;
                if(hash[fruits[left]]==0)
                {
                    hash.erase(fruits[left]);
                }
                left++;
            }
            ret=max(ret,right-left+1);
            right++;
        }
        return ret;
    }
};
